const app = getApp();
// pages/componet/major/major.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    major_array: [],
    showModal: false,
    showModalEdit: false,
    majorName: '',
    majorNameNew: '',
    majorId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let _that = this;
    app.getToken().then(function (){
      _that.loadData();
    });
  },

  loadData: function (){
    const thz = this;
    wx.showLoading({
      title: '加载中'
    });
    wx.request({
      url: app.globalData.requestUrl + 'api/major/lists',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken,
      },
      success: function (ret)
      {
        wx.hideLoading();
        var data = ret.data;
        thz.setData(
          {
            major_array: data.data.lists
          }
        );
      },
      fail: function (e)
      {
        wx.showLoading({
          title: '系统错误请稍后重试',
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //创建专业
  /**
     * 弹窗
     */
  showDialogBtn: function () {
    this.setData({
      showModal: true
    })
  },
  editMajor: function (e) {
    var tid = e.currentTarget.dataset.tid;
    var tname = e.currentTarget.dataset.tname;
    //console.log("000:"+tname)
    this.setData({
      showModalEdit: true,
      majorId: tid,
      majorNameNew: tname

    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    //console.log('xxxxxx');
    this.setData({
      showModal: false
    });
  },
  /**
   * 隐藏模态对话框(编辑)
   */
  hideModalEdit: function () {
    this.setData({
      showModalEdit: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  onCancelEdit: function () {
    this.hideModalEdit();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    var thz = this;
    if (this.data.majorName == '') {
      wx.showToast({
        title: '请输入专业名称',
        icon: 'none',
      });
      return;
    }
    wx.request({
      url: app.globalData.requestUrl + 'api/major/create',
      method: 'POST',
      data: {major_name: this.data.majorName},
      success: function(ret)
      {
        var status = ret.status;
        var data = ret.data;
        thz.setData(
          {
            major_array: data.data.majors
          }
        );
        //成功提示
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 2000
        })

      },
      fail: function(e)
      {
        wx.showLoading({
          title: '系统错误请稍后重试',
        });
      }
    })
    this.hideModal();
  },

  onConfirmEdit: function () {
    var thz = this;
    if (thz.data.majorNameNew == '') {
      wx.showToast({
        title: '请输入专业名称',
        icon: 'none'
      });
      return;
    }
    wx.request({
      url: app.globalData.requestUrl + 'api/major/edit',
      method: 'POST',
      data: { major_name: this.data.majorNameNew, id:this.data.majorId },
      success: function (ret) {
        var status = ret.status;
        var data = ret.data;
        
        //成功提示
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 2000
        });

        thz.onLoad();

      },
      fail: function (e) {
        wx.showLoading({
          title: '系统错误请稍后重试',
        });
      }
    });

    this.hideModalEdit();
  },

  inputChange: function (e)
  {
    this.setData(
      {
        majorName: e.detail.value
      }
    );
  },
  inputChangeEdit: function (e) {
    this.setData(
      {
        majorNameNew: e.detail.value
      }
    );
  },
  deleteMajor: function (e)
  {
    var thz = this;
    var tid = e.currentTarget.dataset.tid;

    wx.showModal({
      title: '提示',
      content: '是否删除分类？',
      success: function (res) 
      {
        if (res.cancel) {
          return;
        }
        wx.request({
          url: app.globalData.requestUrl + 'api/major/delete',
          method: 'POST',
          data: { id: tid },
          success: function (ret) {
            if(ret.data.status == 1) {
              var data = ret.data;
              thz.setData(
                  {
                    major_array: data.data.majors
                  }
              );
            }else{
              wx.showToast({
                title: ret.data.msg,
                icon: 'none',
              });
            }
          },
          fail: function (e) {
            wx.showLoading({
              title: '系统错误请稍后重试',
            });
          }
        }); //end delete
      }
    });
  }

})